<?php
/**
 * Project: HTPHouDuan
 * User: xilibo
 * Date: 2018/1/24
 */

class Mcrypter
{
    protected $cipher;
    protected $mode;
    protected $pad_method;
    protected $secret_key;
    protected $iv;

    public function __construct($key,  $cipher = MCRYPT_3DES, $mode = MCRYPT_MODE_ECB, $iv = '', $method = 'pkcs7')
    {
        $this->secret_key = $key;

        $this->pad_method =$method;

        $this->iv = $iv;

        $this->mode = $mode;

        $this->cipher = $cipher;
    }

    protected function pad_or_unpad($str, $ext)
    {
        if (!is_null($this->pad_method)) {
            
            $func_name = __CLASS__ . '::' . $this->pad_method . '_' . $ext . 'pad';
            
            if (is_callable($func_name)) {
                
                $size = mcrypt_get_block_size($this->cipher, $this->mode);
                
                return call_user_func($func_name, $str, $size);
            }
        }
        
        return $str;
    }

    protected function pad($str)
    {
        return $this->pad_or_unpad($str, '');
    }

    protected function unpad($str)
    {
        return $this->pad_or_unpad($str, 'un');
    }

    public function encrypt($str)
    {
        $str = $this->pad($str);
        
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
        
        if (empty($this->iv)) {

            $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);

        } else {

            $iv = $this->iv;

        }

        $this->secret_key = str_pad($this->secret_key, 24, '0');
        
        mcrypt_generic_init($td, $this->secret_key, $iv);

        $cyper_text = mcrypt_generic($td, $str);

        $rt = base64_encode($cyper_text);

        mcrypt_generic_deinit($td);

        mcrypt_module_close($td);

        return $rt;
    }

    public function decrypt($str)
    {
        $td = mcrypt_module_open($this->cipher, '', $this->mode, '');
        
        if (empty($this->iv)) {
        
            $iv = @mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
            
        } else {
        
            $iv = $this->iv;
            
        }
        
        $this->secret_key = str_pad($this->secret_key, 24, '0');

        mcrypt_generic_init($td, $this->secret_key, $iv);
        
        $decrypted_text = mdecrypt_generic($td, base64_decode($str));
        
        $rt = $decrypted_text;
        
        mcrypt_generic_deinit($td);
        
        mcrypt_module_close($td);
        
        return $this->unpad($rt);
    }

    public static function pkcs7_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        
        return $text . str_repeat(chr($pad), $pad);
    }

    public static function pkcs7_unpad($text)
    {
        $pad = ord($text[strlen($text) - 1]);
        
        if ($pad > strlen($text)) return false;
        
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
        
        return substr($text, 0, -1 * $pad);
    }

    /**
     * 对需要加密的明文进行填充补位
     * @param $text 需要进行填充补位操作的明文
     * @return 补齐明文字符串
     */
    // public static function pkcs7_pad($text, $blocksize = 16)
    // {
    //     $text_length = strlen($text);
    //     //计算需要填充的位数
    //     $amount_to_pad = $blocksize - ($text_length % $blocksize);
    //     if ($amount_to_pad == 0) {
    //         $amount_to_pad = $blocksize;
    //     }
    //     //获得补位所用的字符
    //     $pad_chr = chr($amount_to_pad);
    //     $tmp     = "";
    //     for ($index = 0; $index < $amount_to_pad; $index++) {
    //         $tmp .= $pad_chr;
    //     }

    //     return $text . $tmp;
    // }

    /**
     * 对解密后的明文进行补位删除
     * @param decrypted 解密后的明文
     * @return 删除填充补位后的明文
     */
    // public static function pkcs7_unpad($text)
    // {
    //     $pad = ord(substr($text, -1));
    //     if ($pad < 1 || $pad > 32) {
    //         $pad = 0;
    //     }

    //     return substr($text, 0, (strlen($text) - $pad));
    // }

}

